#include "gd32f4xx.h"
#include "sys_type.h"
#include "systick.h"
#include "bsp_io.h"


void vPowerOnCheck(void)
{
	delay_1ms(2000);	//长按2s开机。按键按住系统有电，2s期间如果松手，会直接断电

/*******************************/
	MCU_POWEREN(ON);
	delay_1ms(10);
	
	VCC_A1000_5V0_EN(ON);
	VCC_EXT_5V0_EN(ON);
	VCC_SYS_3V3_EN(ON);
	delay_1ms(10);
	
	USB30Hub_PWR_EN(ON);
	delay_1ms(10);

	M_4GPWR_EN(ON);
	M_3V3PWR_EN(ON);
/*******************************/

	Work_LED(ON);
	FAN_CTRL(ON);

	GNSS_RSTN_3V3(LOW);
	delay_1ms(200);
	GNSS_RSTN_3V3(HIGH);

	M_4G_PWRKEY(HIGH);
	delay_1ms(600);
	M_4G_PWRKEY(LOW);

	M_4G_RESET_N(HIGH);
	delay_1ms(600);
	M_4G_RESET_N(LOW);

	//等待按键松手
	while(POWER_BUTTON_ST());
}


void vPowerOffCheck(void)
{
	static ULONG wk_tick_start = 0xFFFFFFFF;
	static UCHAR wk_pre_button_st = FALSE;
	ULONG wk_tick_now = HAL_GetTick();
	UCHAR wk_button_st = POWER_BUTTON_ST();

	if((wk_pre_button_st == FALSE) && (wk_button_st == TRUE))
	{
		wk_tick_start = wk_tick_now;
	}

	if(wk_button_st == TRUE)
	{
		if((wk_tick_now - wk_tick_start) >= 5000)
		{
			M_4G_PWRKEY(HIGH);
			delay_1ms(2000);
			M_4GPWR_EN(OFF);
			M_3V3PWR_EN(OFF);
			USB30Hub_PWR_EN(OFF);
			VCC_A1000_5V0_EN(OFF);
			VCC_EXT_5V0_EN(OFF);
			VCC_SYS_3V3_EN(OFF);
			Work_LED(OFF);
			FAN_CTRL(OFF);
			while(POWER_BUTTON_ST());	//等待按键松手
			MCU_POWEREN(OFF);
		}
	}
	else
	{
		wk_tick_start = 0xFFFFFFFF;
	}
	wk_pre_button_st = wk_button_st;
}

